// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期：2024-11-11 21:00:33
// 生成路径: internal/app/system/logic/balance_records.go
// 生成人：王中阳
// desc:余额记录
// company:云南奇讯科技有限公司
// ==========================================================================

package balanceRecords

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/tiger1103/gfast/v3/internal/app/system/consts"
	"github.com/tiger1103/gfast/v3/internal/app/system/dao"
	"github.com/tiger1103/gfast/v3/internal/app/system/model"
	"github.com/tiger1103/gfast/v3/internal/app/system/model/do"
	"github.com/tiger1103/gfast/v3/internal/app/system/service"
	"github.com/tiger1103/gfast/v3/library/liberr"
)

func init() {
	service.RegisterBalanceRecords(New())
}

func New() service.IBalanceRecords {
	return &sBalanceRecords{}
}

type sBalanceRecords struct{}

func (s *sBalanceRecords) List(ctx context.Context, req *model.BalanceRecordsSearchReq) (listRes *model.BalanceRecordsSearchRes, err error) {
	listRes = new(model.BalanceRecordsSearchRes)
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.BalanceRecords.Ctx(ctx).WithAll()
		if req.Id != "" {
			m = m.Where(dao.BalanceRecords.Columns().Id+" = ?", req.Id)
		}
		if req.Userid != "" {
			m = m.Where(dao.BalanceRecords.Columns().Userid+" = ?", gconv.Int(req.Userid))
		}
		if len(req.DateRange) != 0 {
			m = m.Where(dao.BalanceRecords.Columns().CreatedAt+" >=? AND "+dao.BalanceRecords.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
		}
		listRes.Total, err = m.Count()
		liberr.ErrIsNil(ctx, err, "获取总行数失败")
		if req.PageNum == 0 {
			req.PageNum = 1
		}
		listRes.CurrentPage = req.PageNum
		if req.PageSize == 0 {
			req.PageSize = consts.PageSize
		}
		order := "id desc"
		if req.OrderBy != "" {
			order = req.OrderBy
		}
		var res []*model.BalanceRecordsListRes
		err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
		listRes.List = make([]*model.BalanceRecordsListRes, len(res))
		for k, v := range res {
			listRes.List[k] = &model.BalanceRecordsListRes{
				Id:           v.Id,
				Userid:       v.Userid,
				LinkedUserid: v.LinkedUserid,
				Money:        v.Money,
				Balance:      v.Balance,
				CreatedAt:    v.CreatedAt,
			}
		}
	})
	return
}

func (s *sBalanceRecords) GetExportData(ctx context.Context, req *model.BalanceRecordsSearchReq) (listRes []*model.BalanceRecordsInfoRes, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.BalanceRecords.Ctx(ctx).WithAll()
		if req.Id != "" {
			m = m.Where(dao.BalanceRecords.Columns().Id+" = ?", req.Id)
		}
		if req.Userid != "" {
			m = m.Where(dao.BalanceRecords.Columns().Userid+" = ?", gconv.Int(req.Userid))
		}
		if len(req.DateRange) != 0 {
			m = m.Where(dao.BalanceRecords.Columns().CreatedAt+" >=? AND "+dao.BalanceRecords.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
		}
		if req.PageNum == 0 {
			req.PageNum = 1
		}
		if req.PageSize == 0 {
			req.PageSize = consts.PageSize
		}
		order := "id desc"
		if req.OrderBy != "" {
			order = req.OrderBy
		}
		err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&listRes)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
	})
	return
}

// LinkedDataSearch 相关连表查询数据
func (s *sBalanceRecords) LinkedBalanceRecordsDataSearch(ctx context.Context) (res *model.LinkedBalanceRecordsDataSearchRes, err error) {
	res = new(model.LinkedBalanceRecordsDataSearchRes)
	res.LinkedBalanceRecordsUsers, err = s.ListBalanceRecordsUsers(ctx)
	liberr.ErrIsNil(ctx, err, "获取关联表信息失败")
	return
}

func (s *sBalanceRecords) GetById(ctx context.Context, id int) (res *model.BalanceRecordsInfoRes, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.BalanceRecords.Ctx(ctx).WithAll().Where(dao.BalanceRecords.Columns().Id, id).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取信息失败")
	})
	return
}

func (s *sBalanceRecords) Add(ctx context.Context, req *model.BalanceRecordsAddReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.BalanceRecords.Ctx(ctx).Insert(do.BalanceRecords{
			Userid:  req.Userid,
			Money:   req.Money,
			Balance: req.Balance,
		})
		liberr.ErrIsNil(ctx, err, "添加失败")
	})
	return
}

func (s *sBalanceRecords) Edit(ctx context.Context, req *model.BalanceRecordsEditReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.BalanceRecords.Ctx(ctx).WherePri(req.Id).Update(do.BalanceRecords{
			Userid:  req.Userid,
			Money:   req.Money,
			Balance: req.Balance,
		})
		liberr.ErrIsNil(ctx, err, "修改失败")
	})
	return
}

func (s *sBalanceRecords) Delete(ctx context.Context, ids []int) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.BalanceRecords.Ctx(ctx).Delete(dao.BalanceRecords.Columns().Id+" in (?)", ids)
		liberr.ErrIsNil(ctx, err, "删除失败")
	})
	return
}

func (s *sBalanceRecords) ListBalanceRecordsUsers(ctx context.Context) (linkedBalanceRecordsUsers []*model.LinkedBalanceRecordsUsers, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.Users.
			Ctx(ctx).
			Fields(model.LinkedBalanceRecordsUsers{}).Scan(&linkedBalanceRecordsUsers)
		liberr.ErrIsNil(ctx, err)
	})
	return
}
